<script setup lang="ts">
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
// 组件
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import Skeleton from './components/Skeleton.vue'

//接口
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
// import type JkGuessVue from '@/components/JkGuess.vue'
import type { JkGuessInstance } from '@/types/components'

//轮播
const bannerList = ref<BannerItem[]>([])
const getHomeBanner = async () => {
  const res = await getHomeBannerAPI(1)

  bannerList.value = res.result
}

//类型
const categoryList = ref<CategoryItem[]>([])
const getHomeCategory = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}

//推荐
const hotList = ref<HotItem[]>([])
const getHomeHot = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}
// 获取组件类型
const guessRef = ref<JkGuessInstance>()

const onSrolltolower = () => {
  guessRef.value?.getMore()
}
const isLoading = ref(true)
//页面加载钩子
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBanner(), getHomeCategory(), getHomeHot()])
  isLoading.value = false
})
//自定义下拉刷新触发
const enabled = ref(false)
const onRefresherabort = async () => {
  enabled.value = true
  await Promise.all([
    getHomeBanner(),
    getHomeCategory(),
    getHomeHot(),
    guessRef.value?.getStartPullDownRefresh(),
  ])
  enabled.value = false
}
</script>

<template>
  <CustomNavbar></CustomNavbar>
  <!--滚动区域-->
  <scroll-view
    refresher-enabled
    :refresher-triggered="enabled"
    @refresherrefresh="onRefresherabort"
    @scrolltolower="onSrolltolower"
    class="scroll-view"
    scroll-y
  >
    <!--骨架屏-->
    <Skeleton v-if="isLoading"></Skeleton>
    <template v-else>
      <!--轮播图-->
      <JkSwiper :list="bannerList"></JkSwiper>
      <!--类别-->
      <CategoryPanel :list="categoryList"></CategoryPanel>
      <!--推荐-->
      <HotPanel :list="hotList"></HotPanel>
      <!--猜你喜欢-->
      <JkGuess ref="guessRef"></JkGuess>
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  display: flex;
  height: 100%;
  flex-direction: column;

  .scroll-view {
    flex: 1;
  }
}
</style>
